package cn.smbms.contorller.provider;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.smbms.contorller.user.LoginController;
import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.service.provider.ProviderService;
import cn.smbms.service.provider.ProviderServiceImpl;
import cn.smbms.service.role.RoleService;
import cn.smbms.service.user.UserService;
import cn.smbms.service.user.UserServiceImpl;
import cn.smbms.tools.ACT;
import cn.smbms.tools.Constants;
import cn.smbms.tools.EncodingTool;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mysql.jdbc.StringUtils;


@Controller
@RequestMapping("/provider")
public class ProviderController {
	private static Logger logger=Logger.getLogger(LoginController.class);
	
	
	/*
	 * 供应商业务实现类属性对象
	 */
	@Resource(name="providerServiceImpl")
	private ProviderService providerService;
	
	
	
	
	/*供应商方法功能判断*/
	@RequestMapping(value="/entrance",method=RequestMethod.GET)
	public String entrance(
			@RequestParam(value="method",required=true)String method,
			HttpServletRequest request, HttpServletResponse response){
		
		if(method != null && method.equals("query")){
			/*this.query(request, response);*/
			logger.debug("=====================query");
			return "redirect:/provider/getProvider";
		}
		return "";
	}
	
	
	 /*
	  * * 查询供应商
	  */
	 
	@RequestMapping(value="/getProvider",method=RequestMethod.GET)
	public String query(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value="queryProName",required=false)String queryProName1,
			@RequestParam(value="queryProCode",required=false)String queryProCode1)
			throws ServletException, IOException {
		String queryProName =queryProName1;
		String queryProCode = queryProCode1;
		if(StringUtils.isNullOrEmpty(queryProName)){
			queryProName = "";
		}
		if(StringUtils.isNullOrEmpty(queryProCode)){
			queryProCode = "";
		}
		List<Provider> providerList = new ArrayList<Provider>();
		providerList = providerService.getProviderList(queryProName,queryProCode);
		request.setAttribute("providerList", providerList);
		request.setAttribute("queryProName", queryProName);
		request.setAttribute("queryProCode", queryProCode);
		return "providerlist";
	}
	
	/*供应商信息查看*/
	@RequestMapping(value="/view")
	public String getUserById(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value="proid",required=true)String proid1)
			throws ServletException, IOException {
		String id = proid1;
	
		if(!StringUtils.isNullOrEmpty(id)){
			Provider provider = null;
			provider = providerService.getProviderById(id);
			request.setAttribute("provider", provider);
			
		}
		return "providerview";
	}
	
	
	/*供应商提交编辑信息*/
	
	@RequestMapping(value="/userModiy",method=RequestMethod.POST)
	public String modify(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("proName")String proName1,
			@RequestParam("proDesc")String proDesc1,
			@RequestParam("proContact")String proContact1,
			@RequestParam("proFax")String proFax1,
			@RequestParam("id")String id1
			)
			throws ServletException, IOException {
		String proName =proName1;
		String proContact =proContact1;
		String proFax = proFax1;
		String proDesc = proDesc1;
		String id = id1;
		Provider provider = new Provider();
		provider.setProName(proName);
		provider.setId(Integer.valueOf(id));
		provider.setProContact(proContact);
		provider.setProFax(proFax);
		provider.setProDesc(proDesc);
		provider.setModifyBy(((User)request.getSession().getAttribute(Constants.USER_SESSION)).getId());
		provider.setModifyDate(new Date());
		Date da=provider.getModifyDate();
		Date de=new java.sql.Date(da.getTime());
		provider.setModifyDate(de);
		boolean flag = false;
		flag = providerService.modify(provider);
		if(flag){
			return "redirect:/provider/entrance?method=query";
		}else{
			return "providermodify";
		}
	
	}
	
	/*
	 * 查看供应商修改信息页面
	 */
	@RequestMapping(value="/ModiyInfo",method=RequestMethod.GET)
	public String getProviderById(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("proid")String proid1)
			throws ServletException, IOException {
		String id = proid1;
		if(!StringUtils.isNullOrEmpty(id)){
			Provider provider = null;
			provider = providerService.getProviderById(id);
			request.setAttribute("provider", provider);
			
		}
		return "providermodify";
	}
	
	
	/*
	 * 删除供应商
	 */
	@RequestMapping(value="/delProvider",method=RequestMethod.GET)
	@ResponseBody
	public String delProvider(HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam("proid")String proid1)
			throws ServletException, IOException {
		String id = proid1;
		HashMap<String, String> resultMap = new HashMap<String, String>();
		if(!StringUtils.isNullOrEmpty(id)){
			int flag = providerService.deleteProviderById(id);
			if(flag == 0){//删除成功
				resultMap.put("delResult", "true");
			}else if(flag == -1){//删除失败
				resultMap.put("delResult", "false");
			}else if(flag > 0){//该供应商下有订单，不能删除，返回订单数
				resultMap.put("delResult", String.valueOf(flag));
			}
		}else{
			resultMap.put("delResult", "notexit");
		}
		String json=JSONObject.toJSONString(resultMap);
		System.out.println(json);
			return json;
	}
	
	/*
	 * 添加供应商
	 */
	@RequestMapping(value="/addProvider",method=RequestMethod.POST)
	public String add(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("proCode")String proCode1,
			@RequestParam("proName")String proName1,
			@RequestParam("proContact")String proContact1,
			@RequestParam("proPhone")String proPhone1,
			@RequestParam("proAddress")String proAddress1,
			@RequestParam("proFax")String proFax1,
			@RequestParam("proDesc")String proDesc1)
			throws ServletException, IOException {
		String proCode = proCode1;
		String proName = proName1;
		String proContact = proContact1;
		String proPhone = proPhone1;
		String proAddress = proAddress1;
		String proFax = proFax1;
		String proDesc = proDesc1;
		
		Provider provider = new Provider();
		provider.setProCode(proCode);
		provider.setProName(proName);
		provider.setProContact(proContact);
		provider.setProPhone(proPhone);
		provider.setProFax(proFax);
		provider.setProAddress(proAddress);
		provider.setProDesc(proDesc);
		provider.setCreatedBy(((User)request.getSession().getAttribute(Constants.USER_SESSION)).getId());
		provider.setCreationDate(new Date());
		Date da=provider.getCreationDate();
		Date de=new java.sql.Date(da.getTime());
		provider.setCreationDate(de);
		boolean flag = false;
		flag = providerService.add(provider);
		if(flag){
			return "redirect:/provider/entrance?method=query";
		}else{
			return "provideradd";
		}
	}
	
	
	
}
